package com.inews.business.web.excel;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.inews.base.utils.Servlets;
import com.inews.base.vo.ExtData;
import com.inews.business.service.RetireStaffService;
import com.inews.business.vo.RetireStaffVo;

@Component
public class RetireStaffExcelView extends AbstractExcelView {
	
	private static Logger logger = Logger.getLogger(RetireStaffExcelView.class);

	@Autowired
	private RetireStaffService service;

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String jsonFilter = request.getParameter("jsonFilter");
		logger.info("接收到的查询条件json：" + jsonFilter);
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(jsonFilter, "search_");
		ExtData<RetireStaffVo> extData = service.findRetireStaffPage(searchParams, null);
		List<RetireStaffVo> voList = extData.getRoot();

		String fileName = URLEncoder.encode("退休人员信息", "UTF-8");
		// 设置response方式,使执行此controller时候自动出现下载页面,而非直接使用excel打开
		response.setContentType("APPLICATION/OCTET-STREAM");
		response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");

		// 产生Excel表头
		HSSFSheet sheet = workbook.createSheet("退休人员信息");
		HSSFRow header = sheet.createRow(0); // 第0行
		// 产生标题列
		header.createCell(0).setCellValue("养老编号");
		header.createCell(1).setCellValue("工作单位");
		header.createCell(2).setCellValue("姓名");
		header.createCell(3).setCellValue("性别");
		header.createCell(4).setCellValue("身份证号");
		header.createCell(5).setCellValue("退休性质");
		header.createCell(6).setCellValue("退休时间");
		header.createCell(7).setCellValue("退休时职务");
		header.createCell(8).setCellValue("参加工改类别");
		header.createCell(9).setCellValue("国家工资");
		header.createCell(10).setCellValue("国家历年退休增费");
		header.createCell(11).setCellValue("基本退休工资");
		header.createCell(12).setCellValue("退休人员补贴");
		header.createCell(13).setCellValue("养老金合计");

		// 填充数据
		int rowNum = 1;
		for (RetireStaffVo vo : voList) {
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(vo.getRetireId());
			row.createCell(1).setCellValue(vo.getCompanyName());
			row.createCell(2).setCellValue(vo.getStaffName());
			row.createCell(3).setCellValue(vo.getSex());
			row.createCell(4).setCellValue(vo.getIdCardNo());
			row.createCell(5).setCellValue(vo.getRetireType());
			row.createCell(6).setCellValue(vo.getRetireDateFormat());
			row.createCell(7).setCellValue(vo.getRetireJob());
			row.createCell(8).setCellValue(vo.getWorkType());
			row.createCell(9).setCellValue(vo.getCountryPayFormat());
			row.createCell(10).setCellValue(vo.getCountryPayAddFormat());
			row.createCell(11).setCellValue(vo.getAllowanceFormat());
			row.createCell(12).setCellValue(vo.getBasePayFormat());
			row.createCell(13).setCellValue(vo.getSumPayFormat());
		}

	}

}
